<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Import node into current document</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.getelementsbytagnamens.html">DOMDocument::getElementsByTagNameNS</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.load.html">DOMDocument::load</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="domdocument.importnode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMDocument::importNode</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">DOMDocument::importNode</span> &mdash; <span class="dc-title">Import node into current document</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domdocument.importnode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span> <span class="methodname"><strong>DOMDocument::importNode</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span> <code class="parameter">$importedNode</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$deep</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   This function returns a copy of the node to import and associates it with 
   the current document.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domdocument.importnode-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">importedNode</code></em></dt>

     <dd>

      <p class="para">
       The node to import.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">deep</code></em></dt>

     <dd>

      <p class="para">
       If set to <strong><code>TRUE</code></strong>, this method will recursively import the subtree under
       the <em><code class="parameter">importedNode</code></em>.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        To copy the nodes attributes <em><code class="parameter">deep</code></em> needs to be set to <strong><code>TRUE</code></strong>
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domdocument.importnode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The copied node or <strong><code>FALSE</code></strong>, if it cannot be copied.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-domdocument.importnode-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   <a href="class.domexception.html" class="classname">DOMException</a> is thrown if node cannot be imported.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-domdocument.importnode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5286">
    <p><strong>Example #1 <span class="function"><strong>DOMDocument::importNode()</strong></span> example</strong></p>
    <div class="example-contents"><p>
      Copying nodes between documents.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$orgdoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$orgdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;element&gt;&lt;child&gt;text&nbsp;in&nbsp;child&lt;/child&gt;&lt;/element&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;node&nbsp;we&nbsp;want&nbsp;to&nbsp;import&nbsp;to&nbsp;a&nbsp;new&nbsp;document<br /></span><span style="color: #0000BB">$node&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$orgdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"element"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;new&nbsp;document<br /></span><span style="color: #0000BB">$newdoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">formatOutput&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Add&nbsp;some&nbsp;markup<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;someelement&gt;text&nbsp;in&nbsp;some&nbsp;element&lt;/someelement&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;'new&nbsp;document'&nbsp;before&nbsp;copying&nbsp;nodes&nbsp;into&nbsp;it:\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Import&nbsp;the&nbsp;node,&nbsp;and&nbsp;all&nbsp;its&nbsp;children,&nbsp;to&nbsp;the&nbsp;document<br /></span><span style="color: #0000BB">$node&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">importNode</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;And&nbsp;then&nbsp;append&nbsp;it&nbsp;to&nbsp;the&nbsp;"&lt;root&gt;"&nbsp;node<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">documentElement</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nThe&nbsp;'new&nbsp;document'&nbsp;after&nbsp;copying&nbsp;the&nbsp;nodes&nbsp;into&nbsp;it:\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
The &#039;new document&#039; before copying nodes into it:
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;
  &lt;someelement&gt;text in some element&lt;/someelement&gt;
&lt;/root&gt;

The &#039;new document&#039; after copying the nodes into it:
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;
  &lt;someelement&gt;text in some element&lt;/someelement&gt;
  &lt;element&gt;
    &lt;child&gt;text in child&lt;/child&gt;
  &lt;/element&gt;
&lt;/root&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>

 

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.getelementsbytagnamens.html">DOMDocument::getElementsByTagNameNS</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.load.html">DOMDocument::load</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
